package com.shitou.shitoucommons.utils;


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.activation.DataSource;
import javax.mail.internet.MimeMessage;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Map;

/**
 * @auto 石头人
 * @since 2023-3-26
 */
@Component
@Slf4j
public class Mail {

    /**
     * 功能描述: 设置 邮件个发送者，发送密码，以及host
     */
    private JavaMailSenderImpl mailSender(){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setUsername("3076767823@qq.com");
        mailSender.setPassword("ukcmlnmxvxdmdehd");
        mailSender.setHost("smtp.qq.com");
        return mailSender;
    }

    /**
     * 功能描述: 发送简单的邮件.
     */
    @Async
    public void simpleSend(Map<String ,String> map){

        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setSubject(map.get("title"));  //主题
        mailMessage.setText(map.get("text"));   // 内容
        mailMessage.setTo(map.get("to"));
        mailMessage.setFrom(mailSender().getUsername());
        mailSender().send(mailMessage);
        log.info("简单的qq邮件发送成功");
    }

    /**
     * 功能描述: 异步发送复杂邮件
     */
//    @Async
//    public void mimeSend(Map<String,String> map){
//        MimeMessage mailMessage = mailSender().createMimeMessage();
//        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
//
//        try {
//            messageHelper.setSubject(map.get("subject"));
//            messageHelper.setText(map.get("text"),true);
//            messageHelper.setTo(map.get("to"));
//            messageHelper.setFrom(mailSender().getUsername());
//        } catch (MessagingException e) {
//            e.printStackTrace();
//        }
//        mailSender().send(mailMessage);
//        log.info("复杂的QQ邮件发送成功");
//    }


    /**
     * 发送内容中带有图片的邮件
     * @return
     */
    @Async
    @SneakyThrows
    public String sendImageMailMessage(Map<String, String> map, BufferedImage bufferedImage) {

        try {
            MimeMessage mimeMessage = mailSender().createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject(map.get("title"));
            helper.setFrom(mailSender().getUsername());
            helper.setTo(map.get("to"));
            helper.setSentDate(new Date());
            helper.setText("<p>您的验证码</p><p>验证码：</p><img src='cid:p01'/>",true);
            helper.addInline("p01", (Resource) bufferedImage.getSource());

            mailSender().send(mimeMessage);
        } catch (MailException e) {
            e.printStackTrace();
            return "send fail";
        }
        return "send success";

    }


}
